<?php
error_reporting(E_ALL ^ E_NOTICE);

set_time_limit(0);
date_default_timezone_set("PRC"); //timezone

$db1=mysql_connect("localhost","root","root"); 
mysql_select_db('faithwar',$db1); 

//$db2=mysql_connect('114.80.213.44','triniti','triniti'); 
//mysql_select_db('faithwar',$db2); 

$dateTime = date("Y-m-d");

//服务器总玩家数
$Players_total_query = mysql_query("SELECT count(DISTINCT deviceid) FROM tbheartbeat",$db1);

$Players_total = mysql_fetch_array($Players_total_query);

//服务器总玩家数的城堡数量
$Players_cities_total_query = mysql_query("SELECT sum( cities )
        FROM (

        SELECT DISTINCT `a`.`logDate` , `a`.`cities` , `a`.`deviceid`
        FROM `tbheartbeat` `a`
        INNER JOIN (

        SELECT `tbheartbeat`.`deviceid` , Max( `tbheartbeat`.`logDate` ) `mmm`
        FROM `tbheartbeat`
        GROUP BY `tbheartbeat`.`deviceid`
        ) `b` ON `a`.`deviceid` = `b`.`deviceid`
        AND `a`.`logDate` = `b`.`mmm`
        )c ",$db1);

$Players_cities_total = mysql_fetch_array($Players_cities_total_query);
//每天的玩家数
$Players_total_day_query = mysql_query("SELECT count(DISTINCT deviceid) FROM tbheartbeat  where date = '$dateTime'",$db1);

$Players_total_day = mysql_fetch_array($Players_total_day_query);
//每天的玩家城堡数量
$Players_cities_total_day_query = mysql_query("SELECT sum( cities )
        FROM (

        SELECT DISTINCT `a`.`logDate` , `a`.`cities` , `a`.`deviceid`
        FROM `tbheartbeat` `a`
        INNER JOIN (

        SELECT `tbheartbeat`.`deviceid` , Max( `tbheartbeat`.`logDate` ) `mmm`
        FROM `tbheartbeat` WHERE date = '$dateTime'
        GROUP BY `tbheartbeat`.`deviceid`
        ) `b` ON `a`.`deviceid` = `b`.`deviceid`
        AND `a`.`logDate` = `b`.`mmm`
        )c ",$db1);

$Players_cities_total_day = mysql_fetch_array($Players_cities_total_day_query);



   mysql_query("INSERT INTO Players_cities ( date,Players_total,Players_cities_total,Players_total_day,Players_cities_total_day) 
                    VALUES('$dateTime','$Players_total[0]','$Players_cities_total[0]','$Players_total_day[0]','$Players_cities_total_day[0]')",$db1) or die(mysql_error());

 print "Import successful!<br>";

?>